namespace TXLooker.WorkItemExtention.WIQL
{
    using System;

    public class NodeFieldName : NodeItem
    {
        private Direction m_direction;
        private object m_tag;
        private DataType m_type;

        public NodeFieldName(NodeName n) : base(NodeType.FieldName, n.Value)
        {
            base.StartOffset = n.StartOffset;
            base.EndOffset = n.EndOffset;
        }

        public NodeFieldName(string s) : base(NodeType.FieldName, s)
        {
        }

        public override void Bind(IExternal e, NodeTableName tableContext, NodeFieldName fieldContext)
        {
            if (e != null)
            {
                this.m_tag = e.FindField(base.Value, tableContext.Tag);
                Tools.EnsureSyntax(this.m_tag != null, SyntaxError.FieldDoesNotExistInTheTable, this);
                this.m_type = e.GetFieldDataType(this.m_tag);
                Tools.EnsureSyntax(this.m_type != DataType.Unknown, SyntaxError.UnknownFieldType, this);
            }
            base.Bind(e, tableContext, fieldContext);
        }

        public override string ToString()
        {
            string text = Tools.FormatName(base.Value);
            switch (this.m_direction)
            {
                case Direction.Ascending:
                    return (text + " asc");

                case Direction.Descending:
                    return (text + " desc");
            }
            return text;
        }

        public override DataType DataType
        {
            get
            {
                return this.m_type;
            }
        }

        public Direction Direction
        {
            get
            {
                return this.m_direction;
            }
            set
            {
                this.m_direction = value;
            }
        }

        public override bool IsConst
        {
            get
            {
                return false;
            }
        }

        public object Tag
        {
            get
            {
                return this.m_tag;
            }
        }
    }
}

